//
//  EnrollView.m
//  项目三
//
//  Created by rimi1 on 2016/11/30.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "EnrollView.h"
#import "AVRequest+SignIn.h"

static NSInteger spacing_ = 20;

@interface EnrollView ()

@end

@implementation EnrollView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.account];
        [self addSubview:self.password];
        [self addSubview:self.samePassword];
        [self addSubview:self.name];
        [self addSubview:self.boy];
        [self addSubview:self.girl];
        [self addSubview:self.sureButton];
    }
    return self;
}

- (void)enrollSucceed {
    self.name.textField.text = @"";
    self.account.textField.text = @"";
    self.password.textField.text = @"";
    self.samePassword.textField.text = @"";
    
    [self.account.tureOrFalse removeFromSuperview];
    [self.password.tureOrFalse removeFromSuperview];
    [self.samePassword.tureOrFalse removeFromSuperview];
    [self.name.tureOrFalse removeFromSuperview];
    
    self.boy.selected = YES;
    self.girl.selected = NO;
}

#pragma mark - respondsTo..

- (void)respondsToSure:(UIButton *)sender {
    [self endEditing:YES];
    UIImage *image = [UIImage imageNamed:@"对"];
    // 判断是否所有条件都合适
    if ([self.account.tureOrFalse.image isEqual:image] && [self.password.tureOrFalse.image isEqual:image] && [self.samePassword.tureOrFalse.image isEqual:image] && [self.name.tureOrFalse.image isEqual:image]) {
        NSString *str;
        if (self.boy.selected) {
            str = @"男";
        }else {
            str = @"女";
        }
        [self.delegate enrollView:self tapOfSureWithIs:YES account:self.account.textField.text password:self.password.textField.text name:self.name.textField.text sex:str];
        
    }else {
        [self.delegate enrollView:self tapOfSureWithIs:NO account:@"" password:@"" name:@"" sex:@""];
    }
}
// tag 100 = 男  tag 101 = 女
- (void)respondsToSex:(UIButton *)sender {
    [self endEditing:YES];
    if (sender.selected) {
        return;
    }
    
    if (sender.tag == 100) {
        self.girl.selected = NO;
        self.girl.backgroundColor = [UIColor lightGrayColor];
    }else {
        self.boy.selected = NO;
        self.boy.backgroundColor = [UIColor lightGrayColor];
    }
    
    sender.selected = YES;
    sender.backgroundColor = [UIColor orangeColor];
}

#pragma mark - get
// YES为消失 对， NO为出现 错
- (EnrollTextFieldView *)name {
    
    if (!_name) {
        
        CGFloat wSpacing = WXQAdaptionWidth(spacing_/2);
        _name = [[EnrollTextFieldView alloc] initWithFrame:CGRectMake(wSpacing, spacing_/2, self.bounds.size.width - 2 * wSpacing, 0) title:@"昵称" placeholder:@"不得为空" error:@"昵称已存在"];
        _name.maxLength = 15;
        
        __weak EnrollTextFieldView *textField = _name;
        _name.endEdit = ^() {
            NSString *str = textField.textField.text;
            if (str.length != 0 && ![str isEqualToString:@""]) {
                [AVRequest avNameExistWithName:str succeed:^(BOOL succeed, NSError *error) {
                    if (error) {
                        [textField showImageWithTure:NO];
                        [textField showErrorWithTure:YES];
                        return;
                    }
                    // 真则昵称已存在
                    if (succeed) {
                        [textField showImageWithTure:NO];
                        [textField showErrorWithTure:NO];
                    }else {
                        [textField showImageWithTure:YES];
                    }
                }];
            }else {
                [textField showImageWithTure:NO];
            }
        };
        
    }
    return _name;
}

- (EnrollTextFieldView *)account {
    
    if (!_account) {
        
        // 高度已固定
        _account = [[EnrollTextFieldView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.name.frame), CGRectGetMaxY(self.name.frame) + spacing_, CGRectGetWidth(self.name.bounds), 0) title:@"账号" placeholder:@"长度不能少于6位" error:@"账号已存在"];
        _account.maxLength = 15;
        _account.textField.keyboardType = UIKeyboardTypeASCIICapable;
        
        __weak EnrollTextFieldView *textField = _account;
        _account.endEdit = ^() {
            NSString *str = textField.textField.text;
            if (str.length < 6) {
                [textField showImageWithTure:NO];
                [textField showErrorWithTure:YES];
                
            }else {
                // 判断是否存在账号
                [AVRequest avAccountExistWithAccount:str succeed:^(BOOL succeed, NSError *error) {
                    // 当错误存在
                    if (error) {
                        [textField showImageWithTure:NO];
                        [textField showErrorWithTure:YES];
                        return;
                    }
                    // 真则账号已存在
                    if (succeed) {
                        [textField showImageWithTure:NO];
                        [textField showErrorWithTure:NO];
                    }else {
                        [textField showImageWithTure:YES];
                        [textField showErrorWithTure:YES];
                    }
                }];
                
            }
            
        };
        
    }
    return _account;
}

- (EnrollTextFieldView *)password {
    
    if (!_password) {
        
        _password = [[EnrollTextFieldView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.account.frame), CGRectGetMaxY(self.account.frame) + spacing_, CGRectGetWidth(self.account.bounds), 0) title:@"密码" placeholder:@"长度不能少于6位" error:nil];
        _password.textField.secureTextEntry = YES;
        _password.textField.clearsOnBeginEditing = YES;
        _password.maxLength = 15;
        _password.textField.keyboardType = UIKeyboardTypeASCIICapable;
        
        __weak EnrollTextFieldView *textField = _password;
        _password.endEdit = ^() {
            NSString *str = textField.textField.text;
            if (str.length < 6) {
                [textField showImageWithTure:NO];
            }else {
                [textField showImageWithTure:YES];
            }
        };
        
    }
    return _password;
}

- (EnrollTextFieldView *)samePassword {
    
    if (!_samePassword) {
        
        _samePassword = [[EnrollTextFieldView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.password.frame), CGRectGetMaxY(self.password.frame) + spacing_, CGRectGetWidth(self.password.bounds), 0) title:@"再次输入密码" placeholder:@"两次必须密码一致" error:@"两次密码不一致"];
        _samePassword.textField.secureTextEntry = YES;
        _samePassword.textField.clearsOnBeginEditing = YES;
        _samePassword.maxLength = 15;
        _samePassword.textField.keyboardType = UIKeyboardTypeASCIICapable;
        
        __weak EnrollTextFieldView *textField = _samePassword;
        __weak EnrollView *enroll = self;
        _samePassword.endEdit = ^() {
            NSString *str = textField.textField.text;
            if (str.length < 6) {
                [textField showImageWithTure:NO];
                [textField showErrorWithTure:YES];
            }else {
                if ([str isEqualToString:enroll.password.textField.text]) {
                    [textField showImageWithTure:YES];
                    [textField showErrorWithTure:YES];
                }else {
                    [textField showImageWithTure:NO];
                    [textField showErrorWithTure:NO];
                }
            }
            
        };
        
    }
    return _samePassword;
}

- (UIButton *)boy {
    
    if (!_boy) {
        
        _boy = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.samePassword.frame) + spacing_, WXQAdaptionWidth(90), 44)];
        CGPoint point = _boy.center;
        point.x = self.bounds.size.width/3;
        _boy.center = point;
        _boy.layer.cornerRadius = 10;
        _boy.selected = YES;
        _boy.backgroundColor = [UIColor orangeColor];
        [_boy setTitle:@"男" forState:UIControlStateNormal];
        _boy.tag = 100;
        [_boy addTarget:self action:@selector(respondsToSex:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _boy;
}

- (UIButton *)girl {
    
    if (!_girl) {
        
        _girl = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(self.boy.frame), CGRectGetWidth(self.boy.bounds), CGRectGetHeight(self.boy.bounds))];
        CGPoint point = _girl.center;
        point.x = self.bounds.size.width/3 * 2;
        _girl.center = point;
        _girl.layer.cornerRadius = 10;
        _girl.backgroundColor = [UIColor lightGrayColor];
        _girl.tag = 101;
        [_girl setTitle:@"女" forState:UIControlStateNormal];
        [_girl addTarget:self action:@selector(respondsToSex:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _girl;
}

- (UIButton *)sureButton {
    
    if (!_sureButton) {
        
        CGFloat width = 70;
        _sureButton = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.girl.frame) + spacing_, width, width)];
        _sureButton.layer.cornerRadius = width/2;
        _sureButton.backgroundColor = [UIColor orangeColor];
        [_sureButton setTitle:@"注 册" forState:UIControlStateNormal];
        CGPoint point = _sureButton.center;
        point.x = self.bounds.size.width/2;
        _sureButton.center = point;
        [_sureButton addTarget:self action:@selector(respondsToSure:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _sureButton;
}

@end














